home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0038_Faster READLN.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-08-24  |  2.4 KB  |  79 lines

  1. {
  2.   I have been exploring a faster way to read lines from text
  3.    files. This one seems to be 30% faster than readln even with
  4.    a full settextbuffer of $FFFF. However, it only works for
  5.    files smaller than 64K ($FFF1) and all lines, including the
  6.    last one, must end in the CR/LF word (readln recognizes the
  7.    EOF (01Ah) char also as an end of line). Please repost any
  8.    improvements. }
  9.  
  10.    program readtext;
  11.  
  12.    Uses CRT;
  13.  
  14.    const 
  15.      maxsize = $FFF0;
  16.    
  17.    type
  18.      barr    = array[0..maxsize] of byte;
  19.      ptrbarr = ^barr;
  20.  
  21.    var
  22.      f : file;
  23.      s : string;
  24.      p : longint;
  25.      fsiz : longint;
  26.      fbuf : ptrbarr;
  27.    
  28.    function pos13(pnt:pointer): word; assembler;
  29.    asm
  30.      les di,[pnt]              {load pointer in es:di}
  31.      mov cx,$00FF              {load maximum size to scan in cx}
  32.      mov bx,cx                 {save maximum size to scan in bx}
  33.      mov al,$0D                {load in al byte to match = 0Dh}
  34.      cld                       {increment di}
  35.      repne scasb               {search loop}
  36.      je  @found                {jump if found}
  37.      mov ax,0                  {if not found report result = 0}
  38.      jmp @fin                  {goto end}
  39.      @found:                   {if found...}
  40.      sub bx,cx                 {get position matched}
  41.      mov ax,bx                 {report result = position matched}
  42.      @fin:
  43.    end;
  44.    
  45.    procedure readx(fbuf:ptrbarr;var s:string;var p:longint);
  46.    var
  47.      q : word;
  48.      b : ptrbarr;
  49.    begin
  50.      b:= addr(fbuf^[p]);       {point to first byte in remaining block}
  51.      q:= pos13(b);             {get position of first $0D occurence}
  52.      move(b^,s[1],pred(q));    {transfer preceeding bytes to string}
  53.      s[0]:= char(pred(q));     {assign size byte to Pascal string}
  54.      inc(p,succ(q));           {adjust pointer skipping 1 byte ($0A)}
  55.    end;
  56.  
  57.    begin
  58.      ClrScr;
  59.      if paramcount = 0 then
  60.         BEGIN
  61.         writeLn( 'Enter FILENAME on commandline');
  62.         halt;
  63.         END;
  64.      assign(f,paramstr(1));
  65.      reset(f,1);
  66.      fsiz:= filesize(f);
  67.      if fsiz > maxsize then halt;
  68.      getmem(fbuf,fsiz);
  69.      blockread(f,fbuf^,fsiz);
  70.      close(f);
  71.      p := 0;                   {initialize pointer to position in fbuf^}
  72.      while p < fsiz do begin
  73.        readx(fbuf,s,p);
  74.        writeln(s);
  75.      end;
  76.      dispose(fbuf);
  77.    end.
  78.  
  79.